﻿//-----------------------------------------------------------------------
// <copyright file="Circle2D.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Common.Math
{
    using System;

    /// <summary>
    /// Circle structure.
    /// </summary>
    public struct Circle2D
    {
        #region Fields - Private

        /// <summary>
        /// Radius of the circle.
        /// </summary>
        private float radius;

        /// <summary>
        /// Center position of the circle..
        /// </summary>
        private Vector2D pos;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor which takes a position and radius.
        /// </summary>
        /// <param name="pos">Rectnagle position.</param>
        /// <param name="r">Circle radius.</param>
        public Circle2D(Vector2D pos, float r)
        {
            if (r < 0.0f)
            {
                throw new ArgumentException("Radius must be >= 0.0f");
            }

            this.pos = pos;
            this.radius = r;
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets or sets the center position of the circle.
        /// </summary>
        public Vector2D Pos
        {
            get
            {
                return this.pos;
            }

            set
            {
                this.pos = value;
            }
        }

        /// <summary>
        /// Gets or sets the radius of the circle.
        /// </summary>
        public float Radius
        {
            get
            {
                return this.radius;
            }

            set
            {
                if (value < 0.0f)
                {
                    throw new ArgumentException("Radius must be >= 0.0f");
                }

                this.radius = value;
            }
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Tests to see if the circle contains the passed point.
        /// </summary>
        /// <param name="pt">Point to test.</param>
        /// <returns>Returns true if the point is in the circle.</returns>
        public bool Contains(Vector2D pt)
        {
            Vector2D toTestPt = pt - this.pos;

            return toTestPt.Dot(toTestPt) <= (this.radius * this.radius);
        }

        #endregion
    }
}
